/**
 * 文件：InsertCoursePanel.java
 * 说明：用于添加课程
 * 作者：古通基
 * 时间：1211-09-13
 */

package com.java.component;

import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class InsertCoursePanel extends JPanel implements ActionListener,MouseListener
{
	private static final long serialVersionUID = -8284021712944704815L;
	private JLabel lno = new JLabel("课  程  号：");                                                  //静态标签
	private JLabel lname = new JLabel("课  程  名：");
	private JLabel lteacher = new JLabel("授课老师：");              
	private JLabel lcredit = new JLabel("学        分：");        
	private JLabel ltime = new JLabel("课        时：");      
	private JLabel laddress = new JLabel("上课地点：");      	

	private JTextField no = new JTextField("请输入课程号",10);                                //文本输入
	private JTextField name = new JTextField("请输入课程名",10); 
	private JTextField teacher = new JTextField("请输入老师名字",10);
    String[]  credits = {"1","2","3","4","5"};
	private JComboBox credit= new JComboBox(credits);  
	String[]  times = {"16","32","48","64","72"};
	private JComboBox time= new JComboBox(times);  
    String[]  addresses = {"二区","三区","四区","五区"};
    private JComboBox address = new JComboBox(addresses);   
    
    private JButton insert = new JButton("添加");                                   //声明按钮
    private JButton reset = new JButton("重置");
    
    private JPanel JPcomon1 = new JPanel();                                          //声明面板
    private JPanel JPcomon2 = new JPanel();
    private JPanel JPcomon3 = new JPanel();
    private JPanel JPcomon4 = new JPanel();
    private JPanel JPcomon5 = new JPanel();
    private JPanel JPcomon6 = new JPanel();
    private JPanel JPbutton = new JPanel();
    
    public InsertCoursePanel()                                                                 //构造函数
    {
    	lno.setHorizontalAlignment(SwingConstants.CENTER);
    	lname.setHorizontalAlignment(SwingConstants.CENTER);
    	lteacher.setHorizontalAlignment(SwingConstants.CENTER);
    	lcredit.setHorizontalAlignment(SwingConstants.CENTER);
    	ltime.setHorizontalAlignment(SwingConstants.CENTER);
    	laddress.setHorizontalAlignment(SwingConstants.CENTER);
   	
    	JPcomon1.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon1.add(lno);
    	JPcomon1.add(no);
    	JPcomon2.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon2.add(lname);
    	JPcomon2.add(name);
    	JPcomon3.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon3.add(lteacher);
    	JPcomon3.add(teacher);
    	JPcomon4.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon4.add(lcredit);
    	JPcomon4.add(credit);
    	JPcomon5.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon5.add(ltime);
    	JPcomon5.add(time);
    	JPcomon6.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon6.add(laddress);
    	JPcomon6.add(address);
    	
    	JPbutton.setLayout(new FlowLayout(FlowLayout.CENTER,12,5));
    	JPbutton.add(insert);
    	JPbutton.add(reset);
    	
    	this.setLayout(new GridLayout(10,1));
    	this.add(JPcomon1);
    	this.add(JPcomon2);
    	this.add(JPcomon3);
    	this.add(JPcomon4);
    	this.add(JPcomon5);
    	this.add(JPcomon6);
    	this.add(JPbutton);
    	
    	insert.addActionListener(this);                                                         //事件监听        
    	reset.addActionListener(this);
    	no.addMouseListener(this);
    	name.addMouseListener(this);
    	teacher.addMouseListener(this);
    }
    
	public void actionPerformed(ActionEvent e)
	{
		if(e.getActionCommand().equals("添加"))
		{
			AddToDataBase();
			insert.setEnabled(false);
		}
		else if(e.getActionCommand().equals("重置"))
		{
			Reset();
			insert.setEnabled(true);
		}
	}
	
	private void AddToDataBase()
	{		
		try
		{
			BaseSQL course = null;
			course = new BaseSQL();
			String sql = "insert into course values('"+Integer.parseInt(no.getText())+"','"+name.getText()+"','"+teacher.getText()+"','"+Integer.parseInt((String) credit.getSelectedItem())+"','"+Integer.parseInt((String) time.getSelectedItem())+"','"+address.getSelectedItem()+"')";
		    course.executeDML(sql);
		    JOptionPane.showMessageDialog(this, "已成功添加课程号为："+Integer.parseInt(no.getText())+" 的课程！", "课程添加",JOptionPane.INFORMATION_MESSAGE);
		}
		catch(SQLException evt)
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		} 
		catch (ClassNotFoundException evt) 
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		}	
		catch (NumberFormatException evt)  
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		}
	}
	
	private void Reset()
	{
		no.setText("");
		name.setText("");
		teacher.setText("");
	}
	
    public void mouseClicked(MouseEvent e) { }                                      //鼠标事件
    public void mouseReleased(MouseEvent e) { }
    public void mouseEntered(MouseEvent e) 
    {
    	if(no.getText().equals("请输入课程号"))
    	{    		
    		no.setText("");
    		this.setVisible(true);
    	}
    	else if(name.getText().equals("请输入课程名"))
    	{
    		name.setText("");
    		this.setVisible(true);
    	}
    	else if(teacher.getText().equals("请输入老师名字"))
    	{
    		teacher.setText("");
    		this.setVisible(true);
    	}
    }
    public void mousePressed(MouseEvent e) { }
    public void mouseExited(MouseEvent e) { }
}

